﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Mangox.Common
{
    public class DbHelperAccess
    {
        public DbHelperAccess()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        //获取连接字符串
        static string constr = GetStr();
        /// <summary>
        /// 链接ACCESS数据库方法
        /// </summary>
        /// <returns></returns>
        public static string GetStr()
        {
            //string path = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + System.Net.Mime.MediaTypeNames.Application.StartupPath + "\\DB\\stockDB.mdb;Persist Security Info=False";
            string path = string.Format(ConfigurationManager.AppSettings["DBAccess"], Directory.GetCurrentDirectory());
            return path;
        }

        /// <summary>
        /// 这里使用静态变量 再批量时提升效率，不会被锁定
        /// </summary>
        static private OleDbConnection connection;
        public OleDbConnection Connection
        {
            get
            {
                if (connection == null)
                {
                    connection = new OleDbConnection(constr);
                    connection.Open();
                }
                else if (connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                }
                else if (connection.State == ConnectionState.Broken)
                {
                    connection.Close();
                    connection.Open();
                }
                return connection;
            }

        }


        /// <summary>
        /// 数据的增 删 改 返回受影响的行数
        /// </summary>
        /// <param name="OleDb"></param>
        /// <returns></returns>
        public int ExecuteCommand(string OleDb)
        {
            OleDbCommand cmd = new OleDbCommand(OleDb, Connection);
            return Convert.ToInt32(cmd.ExecuteNonQuery());
        }

        /// <summary>
        /// 传入参数   数据的增 删 改 返回受影响的行数
        /// </summary>
        /// <param name="OleDb"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public int ExecuteCommand(string OleDb, params OleDbParameter[] values)
        {
            OleDbCommand cmd = new OleDbCommand(OleDb, Connection);
            cmd.Parameters.AddRange(values);
            return Convert.ToInt32(cmd.ExecuteNonQuery());
        }

        /// <summary>
        /// 根据传入OleDb语句来查询获得数据集
        /// </summary>
        /// <param name="OleDb"></param>
        /// <returns></returns>
        public DataSet GetInfoDateSet(string OleDb)
        {
            OleDbCommand cmd = new OleDbCommand(OleDb, Connection);
            OleDbDataAdapter sda = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            sda.Dispose();
            return ds;
        }

        /// <summary>
        /// 根据传入OleDb语句和参数来查询获得数据集
        /// </summary>
        /// <param name="OleDb"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public DataSet GetInfoDateSet(string OleDb, params OleDbParameter[] values)
        {
            OleDbCommand cmd = new OleDbCommand(OleDb, Connection);
            cmd.Parameters.AddRange(values);
            OleDbDataAdapter sda = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);

            return ds;
        }

        /// <summary>
        /// 根据传入OleDb语句来查询获得数据读取对象
        /// </summary>
        /// <param name="OleDb"></param>
        /// <returns></returns>
        public OleDbDataReader GetInfoDataReader(string OleDb)
        {
            OleDbCommand cmd = new OleDbCommand(OleDb, Connection);
            return cmd.ExecuteReader();
        }


        /// <summary>
        /// 根据传入OleDb语句和参数来查询获得数据读取对象
        /// </summary>
        /// <param name="OleDb"></param>
        /// <returns></returns>
        public OleDbDataReader GetInfoDataReader(string OleDb, params OleDbParameter[] values)
        {
            OleDbCommand cmd = new OleDbCommand(OleDb, Connection);
            cmd.Parameters.AddRange(values);
            return cmd.ExecuteReader();
        }

        //再增加一个接收数组的操作

        /// <summary>
        /// 批量数据的插入
        /// </summary>
        /// <param name="sqlArray"></param>
        //public static void InsertToDataByBatch(String[] sqlArray)
        //{
        //    try
        //    {
        //        OleDbConnection aConnection = new OleDbConnection(constr);
        //        aConnection.Open();
        //        OleDbTransaction transaction = Connection.BeginTransaction();

        //        OleDbCommand aCommand = new OleDbCommand();
        //        aCommand.Connection = aConnection;
        //        aCommand.Transaction = transaction;
        //        for (int i = 0; i < sqlArray.Length; i++)
        //        {
        //            aCommand.CommandText = sqlArray[i];
        //            aCommand.ExecuteNonQuery();
        //        }
        //        transaction.Commit();
        //        aConnection.Close();
        //    }
        //    catch (Exception e)
        //    {
        //    }
        //}

    }
}
